﻿//-------------------------------------------------------------------
//	Copyright (c) 2010-2013  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_STL_Ext.h".
//-------------------------------------------------------------------

#include "GLESTexture.h"

namespace UX_STL
{
namespace GLESshell
{


//-------------------------- GLSamplerSW --------------------------//
/*
=================================================
	применить сэмплер к текстуре
=================================================
*/
	void GLESSamplerSW::Bind(uint8 unit) const
	{
		GLenum	e_target = SMAN()->GetTexTarget( unit );
		SMAN()->ActiveTexture( unit );
		
		GL_CALL( glTexParameteri( e_target, GL_TEXTURE_WRAP_S, _sSrc.sWrap.s ) );
		GL_CALL( glTexParameteri( e_target, GL_TEXTURE_WRAP_T, _sSrc.sWrap.t ) );

		if ( OES_texture_3D::IsSupported() )
			GL_CALL( glTexParameteri( e_target, OES_texture_3D::GL_TEXTURE_WRAP_R, _sSrc.sWrap.r ) );

		GL_CALL( glTexParameteri( e_target, GL_TEXTURE_MIN_FILTER, _sSrc.sFilter.min ) );
		GL_CALL( glTexParameteri( e_target, GL_TEXTURE_MAG_FILTER, _sSrc.sFilter.mag ) );
		
		if ( EXT_shadow_samplers::IsSupported() )
		{
			GL_CALL( glTexParameteri( e_target, EXT_shadow_samplers::GL_TEXTURE_COMPARE_MODE,
									  _sSrc.eCompareRTT == GL_NONE ? GL_NONE :
									  EXT_shadow_samplers::GL_COMPARE_REF_TO_TEXTURE ) );
			
			if ( _sSrc.eCompareRTT != GL_NONE )
				GL_CALL( glTexParameteri( e_target, EXT_shadow_samplers::GL_TEXTURE_COMPARE_FUNC, _sSrc.eCompareRTT ) );
		}

		if ( EXT_texture_filter_anisotropic::IsSupported() )
			GL_CALL( glTexParameteri( e_target, EXT_texture_filter_anisotropic::GL_TEXTURE_MAX_ANISOTROPY, _sSrc.uAnisotropy ) );
	}

	void GLESSamplerSW::Bind() const
	{
		Bind( SMAN()->GetCurrentLayer() );
	}

	void GLESSamplerSW::Unbind(uint8 unit) const
	{
		// not supported
	}

	void GLESSamplerSW::Unbind() const
	{
		// not supported
	}

/*
=================================================
	создать софтварный сэмплер
=================================================
*/
	bool GLESSamplerSW::Create(const GLESSamplerSrc &sSource)
	{
		_sSrc = sSource;

		if ( EXT_texture_filter_anisotropic::IsSupported() ) {
			//if ( _sSrc.uAnisotropy > C_GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT )
			//	_sSrc.uAnisotropy = C_GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT;
		}
		else
			_sSrc.uAnisotropy = 0;

		return true;
	}

/*
=================================================
	удалить софтварный сэмплер
=================================================
*/
	void GLESSamplerSW::Destroy()
	{
		_sSrc = GLESSamplerSrc();
	}
	
/*
=================================================
	установить сэмплер для текстуры
=================================================
*/
	bool GLESSamplerSW::Apply(const GLESTexture *pTex) const
	{
		return pTex->ApplySampler( _sSrc );
	}

	
//-------------------------------------------------------------------

}	// GLESshell
}	// UX_STL